package com.xx.controller.xunyou.manager;

import com.xx.admin.entity.AdminUser;
import com.xx.controller.util.BaseController;
import com.xx.springBootUtil.util.ObjectHelper;
import com.xx.springBootUtil.validate.ValidateWriteInPost;
import com.xx.util.page.PageInfo;
import com.xx.util.result.JsonUtil;
import com.xx.xunyou.entity.area.XunyouArea;
import com.xx.xunyou.entity.item.XunyouItemType;
import com.xx.xunyou.service.AreaService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: xunyou-parent
 * @author: zhl
 * @create: 2019-04-18
 **/
@RestController
@ApiIgnore
public class AreaController  extends BaseController {

    @Autowired
    private AreaService areaService;

    @Resource
    PageInfo pageInfo;
    /**
     * 查询
     *
     * @param areaCode
     * @param areaName
     * @param modelAndView
     * @return
     * @throws NumberFormatException
     * @throws Exception
     */
    @RequestMapping("manager/area/query")
    public ModelAndView queryList(String areaCode, String areaName, ModelAndView modelAndView) throws Exception{


        Map<String, Object> map = new HashMap<>();
        map.put("areaCode", areaCode);
        map.put("areaName", areaName);
        Integer totalRow = areaService.queryListCountToManager(map);
        pageInfo.format(totalRow, request); //默认15页
        map.put("index", pageInfo.getPageSize()*(pageInfo.getPageNumber()-1));
        map.put("last", pageInfo.getPageSize());
        List<Map> list = areaService.queryListToManager(map);

        modelAndView.addObject("map", map);
        modelAndView.addObject("list", list);
        modelAndView.addObject("pageInfo", pageInfo);
        modelAndView.setViewName("manager/area/areaList");
        return modelAndView;
    }

    /**
     * 添加信息
     * @return
     * @throws Exception
     */
    @ValidateWriteInPost(parameter={"areaName","areaCode"},msg={"分类名称不能为空！","分类关键字不能为空！"})
    @RequestMapping("manager/area/insert")
    public ModelAndView insert(XunyouArea xunyouArea) throws Exception{

        if(ObjectHelper.getOrPost(request)){
            return new ModelAndView("manager/area/areaInsert");
        }else{
            AdminUser adminUser = sessionAdminUser();
            xunyouArea.setUserId(adminUser.getUserId());
            xunyouArea.setCreateTime(new Date());
            areaService.insert(xunyouArea);

            responseJson(JsonUtil.jsonRefresh("添加成功"));
        }
        return null;
    }

}
